package com.itgaoshu.hospitalproject.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itgaoshu.hospitalproject.bean.Warehuose;
import com.itgaoshu.hospitalproject.service.WarehuoseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("warehouse")
public class WarehuoseController {
    @Autowired
    private WarehuoseService warehuoseService;

    //查询所有仓库的方法
    @RequestMapping("findAllWarehuose")
    @ResponseBody
    public Object findAllWarehuose(String supplierName,Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        List<Warehuose> warehuoses = warehuoseService.findAllWarehuose(supplierName);
        PageInfo pageInfo = new PageInfo(warehuoses);
        Map<String,Object> tableDate = new HashMap<>();
        tableDate.put("msg","");
        tableDate.put("code",0);
        tableDate.put("data",pageInfo.getList());
        tableDate.put("count",pageInfo.getTotal());
        return  tableDate;
    }


    //添加仓库的方法
    @ResponseBody
    @RequestMapping("addWarehuose")
    public Object addWarehuose(Warehuose warehuose){
        int count = warehuoseService.count(warehuose);
        if(count==0){
            int result = warehuoseService.addWarehuose(warehuose);
            if(result>0){
                return "添加成功";
            }else{
                return "添加失败";
            }
        }else{
            return "这个仓库已经存在了";
        }

    }


}
